local LeftView = MClass("LeftView")

local NodePool = custom.NodePool:getInstance()

function LeftView:ctor()
	--self.root 默认为根节点
   	self.root:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL)
    self.innerContainer = self.root:getInnerContainer()

    -- 9 == palyernode panel 子节点个数
    self._mulLayerParentNode = custom.MultipleLayersParentNode:create(9)
    self.innerContainer:addChild(self._mulLayerParentNode)
   	self:init()
end

function LeftView:init()
	
end

function LeftView:addView(nodes)
    self:test(nodes)
end

function LeftView:clear()
    -- local innerContainer = self:getInnerContainer()
    local childs = self._mulLayerParentNode:getChildren() or {}
    for _,child in ipairs(childs) do
        if not cc.isDead(child) then
            NodePool:put(child,"PlayerNode")
            child:removeFromParent(false)
        end
    end
end

function LeftView:test(nodes)
    -- local innerContainer = self:getInnerContainer()
    local addedNodes = self._mulLayerParentNode:getChildren() or {}
    --print("=============addPlayerNode nodes count:",#addedNodes)
    local newNodes= {}
    for k,child in pairs(addedNodes) do
        child:retain()
        table.insert(newNodes, child)
        self._mulLayerParentNode:removeChild(child, false)
    end

    if nodes then
        for k,child in pairs(nodes) do
            table.insert(newNodes, child)
        end
    end

    table.sort(newNodes,function(l,r) 
        return l:getIndex() > r:getIndex()
    end)

	if #newNodes > 0 then
        display.ScrollViewFill(self.root, newNodes, 
        {
                --自动间距
                autoGap = false,
                --宽间距
                widthGap = 5,
                --高间距
                heightGap = 5,
                --自动行列
                autoTable = false,
                --行数目
                rowCount = 3,
                --列数目
                cellCount = 3,
                --填充项大小
                itemSize = newNodes[1]:getContentSize(),
                --容器大小根据item多少决定
                autoSize = true,
                --自右向左开始
                rightToLeft = false,
                --zorder 降序
                zorderDesc = true,
                parent = self._mulLayerParentNode,
        })
    end
	for k,child in pairs(addedNodes) do
        child:release()
    end
end

return LeftView